#!/bin/bash
set -e
set -u
#
# Run a simple backup
#   then restore it.
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-to-pool1
#shellcheck source=../environment.in
. ./environment

JobName=backup-susan-fd
#shellcheck source=../scripts/functions
. "${rscripts}"/functions
"${rscripts}"/cleanup
"${rscripts}"/setup


# Directory to backup.
# This directory will be created by setup_data "$@"().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data "$@"

start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
setdebug level=100 storage=File
run job=backup-to-pool1 level=Full yes
run job=backup-to-pool2 level=Full yes
run job=backup-to-pool3 level=Full yes
run job=backup-to-pool1 level=Full yes
run job=backup-to-pool2 level=Full yes
run job=backup-to-pool3 level=Full yes
status storage=File
wait
messages
@#
@# now do a restore
@#
@$out $tmp/log2.out
restore client=susan-fd fileset=SelfTest where=$tmp/susan-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_susan "$@"
check_for_zombie_jobs storage=File
stop_susan

check_two_logs
check_restore_diff ${BackupDirectory}

#make sure all MultiFileStorage devices are used
grep "Using Device \"MultiFileStorage0001\"" "${tmp}"/log1.out >/dev/null 2>&1 &&
grep "Using Device \"MultiFileStorage0002\"" "${tmp}"/log1.out >/dev/null 2>&1 &&
grep "Using Device \"MultiFileStorage0003\"" "${tmp}"/log1.out >/dev/null 2>&1
if test $? -ne 0 ; then
  echo "Not all MultiFileStorage devices are used."
  estat=1;
fi

end_test

